package org.bbelovic.devel.exercises;

import static java.util.Collections.emptyList;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

public class WeatherKataLineProcessor implements LineProcessor {

    private static final int MINIMAL_TOKEN_COUNT = 13;
    private static final String TOKEN_DELIMITER_PATTERN = "\\s+";
    private static final String TOKEN_VALIDATING_PATTERN = "\\d+";

    public List<String> processLine(final String inputLine) {
        if (inputLine == null)
            throw new IllegalArgumentException("Input line is null");
        final String [] tokens = inputLine.trim().split(TOKEN_DELIMITER_PATTERN);
        if (tokens.length < MINIMAL_TOKEN_COUNT) {
            return emptyList();
        }
        if (!Pattern.matches(TOKEN_VALIDATING_PATTERN, tokens[0]) ||
            !Pattern.matches(TOKEN_VALIDATING_PATTERN, tokens[1]) || 
            !Pattern.matches(TOKEN_VALIDATING_PATTERN, tokens[2]))
        {
            return emptyList();
        }
        return toList(tokens);
    }

    private List<String> toList(final String [] tokens) {
        final List<String> aList = new ArrayList<String>();
        for (String eachToken: tokens) {
            aList.add(eachToken);
        }
        return aList;
    }
}
